package com.aim.questionnaire.service;

import com.aim.questionnaire.common.utils.DateUtil;
import com.aim.questionnaire.common.utils.UUIDUtil;
//import com.aim.questionnaire.config.shiro.SysUserService;
//import com.aim.questionnaire.config.shiro.entity.UserOnlineBo;
import com.aim.questionnaire.dao.QuestionnaireEntityMapper;
import com.aim.questionnaire.dao.UserEntityMapper;
import com.aim.questionnaire.dao.entity.UserEntity;
//import com.alibaba.fastjson.JSONArray;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.ws.Action;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by wln on 2018\8\9 0009.
 */
@Service
public class UserService {

    @Autowired
    private UserEntityMapper userEntityMapper;

    //@Autowired
    //private SysUserService sysUserService;

    @Autowired
    private QuestionnaireEntityMapper questionnaireEntityMapper;

    /**
     * 查询用户列表（模糊搜索）
     * @param map
     * @return
     */
    public PageInfo queryUserList(Map<String,Object> map) {

        List<Map<String,Object>> resultList = new ArrayList<Map<String,Object>>();
        int pageNum = Integer.parseInt(map.get("pageNum").toString());
        int pageSize = Integer.parseInt(map.get("pageSize").toString());
        PageHelper.startPage(pageNum, pageSize);
        List<Map<String,Object>> result = userEntityMapper.queryUserList(map);
        //获取角色
//        for(Map<String,Object> userMap : result) {
//            userMap.put("password","******");
//            //时间转换
//            Date startTime = DateUtil.stringToSqlDate(userMap.get("startTime").toString(),DateUtil.DEF_DATE_FORMAT_STR);
//            Date endTime = DateUtil.stringToSqlDate(userMap.get("endTime").toString(),DateUtil.DEF_DATE_FORMAT_STR);
//            String startTimeStr = DateUtil.dateToString(startTime,DateUtil.DEF_DATE_FORMAT_STR);
//            String endTimeStr = DateUtil.dateToString(endTime,DateUtil.DEF_DATE_FORMAT_STR);
//            userMap.put("startTime",startTimeStr);
//            userMap.put("endTime",endTimeStr);
//            resultList.add(userMap);
//        }
        PageInfo pageInfo = new PageInfo(result);
       
        return pageInfo;
    }

    /**
     * 创建用户的基本信息
     * @param map
     * @return
     */
    public int addUserInfo(Map<String,Object> map) {

        if(map.get("username") != null) {
            int userResult = userEntityMapper.queryExistUser(map);
            if(userResult != 0) {
                //用户名已经存在
                return 3;
            }
        }

        String id = UUIDUtil.getOneUUID();
        map.put("id",id);
        //创建时间
        Date date = DateUtil.getCreateTime();
        map.put("creationDate",date);
        map.put("lastUpdateDate",date);
        //创建人
        String user = map.get("username").toString();
        map.put("createdBy",user);
        map.put("lastUpdatedBy",user);
        //前台传入的时间戳转换
        String startTimeStr = map.get("startTime").toString();
        String endTimeStr = map.get("stopTime").toString();
        Date startTime = DateUtil.getMyTime(startTimeStr);
        Date endTime = DateUtil.getMyTime(endTimeStr);
        map.put("startTime",startTime);
        map.put("stopTime",endTime);

        int result = userEntityMapper.addUserInfo(map);
        return result;
    }

    /**
     * 编辑用户的基本信息
     * @param map
     * @return
     */
    public int modifyUserInfo(Map<String, Object> map) {
        if(map.get("username") != null) {
            int userResult = userEntityMapper.queryExistUser(map);
            if(userResult != 0) {
                //用户名已经存在
                return 4;
            }
        }

        if(map.get("oldPassword") != null) {
            int passwordResult = userEntityMapper.queryUserPassword(map);
            if(passwordResult == 0) {
                //密码错误
                return 3;
            }
        }
        if(map.get("startTime") != null) {
            String startTimeStr = map.get("startTime").toString();
            Date startTime = DateUtil.getMyTime(startTimeStr);
            map.put("startTime",startTime);
        }
        if(map.get("stopTime") != null) {
            String endTimeStr = map.get("stopTime").toString();
            Date endTime = DateUtil.getMyTime(endTimeStr);
            map.put("stopTime",endTime);
        }
        Date date = DateUtil.getCreateTime();
        map.put("lastUpdateDate",date);
        String user = map.get("user").toString();
        map.put("lastUpdatedBy",user);
        int result = userEntityMapper.modifyUserInfo(map);
//        int resultQue = questionnaireEntityMapper.modifyQuestionCreadBy(map);
        return result;
    }

    /**
     * 修改用户状态
     * @param map
     * @return
     */
    public int modifyUserStatus(Map<String, Object> map) {
        // int result= userEntityMapper.modifyUserStatus(map);
        // if(map.get("status").toString().equals("0")) {
        //     List<UserOnlineBo> userSessionInfo = sysUserService.getUserSessionInfo();
        //     for(UserOnlineBo userOnlineBo : userSessionInfo) {
        //         if(userOnlineBo.getId().equals(map.get("id").toString())) {
        //             sysUserService.kickout(userOnlineBo.getSessionId());
        //         }
        //     }
        // }
        // if(map.get("id").toString().equals(map.get("sessionUserId").toString())) {
        //     return 3;//关闭的是自己的权限
        // }else {
        //     return result;
        // }
        return 0;
    }

    /**
     * 根据id查询用户信息
     * @param userEntity
     * @return
     */
    public Map<String,Object> selectUserInfoById(UserEntity userEntity) {
        Map<String,Object> result = userEntityMapper.selectUserInfoById(userEntity);
        //时间转换
        Date startTime = DateUtil.stringToSqlDate(result.get("startTime").toString(),DateUtil.DEF_DATE_FORMAT_STR);
        Date stopTime = DateUtil.stringToSqlDate(result.get("stopTime").toString(),DateUtil.DEF_DATE_FORMAT_STR);
        String startTimeStr = DateUtil.dateToString(startTime,DateUtil.DEF_DATE_FORMAT_STR);
        String stopTimeStr = DateUtil.dateToString(stopTime,DateUtil.DEF_DATE_FORMAT_STR);
        result.put("startTime",startTimeStr);
        result.put("stopTime",stopTimeStr);
        return result;
    }

    /**
     * 删除用户信息
     * @param userEntity
     * @return
     */
    public int deteleUserInfoById(UserEntity userEntity) {
        //查询用户创建的项目
        int userProject = userEntityMapper.queryProjectByUser(userEntity.getId());
        if(userProject != 0) {
            //用户不可删除，请先删除用户创建的项目、问卷、模板
            return 3;
        }
        int result = userEntityMapper.deteleUserInfoById(userEntity);
        return result;
    }
}
